#include "Main/stdafx.h"
#include "FKNNGameScence.h"
#include "FKNNPlayer.h"
#include "Game/GameLib.h"
#include "CMD_FKOx.h"
#include "FKNNPlayer.h"

class FKNNTouchScence
	:public cocos2d::Layer
{
public:
	virtual bool init()
	{
		if ( !cocos2d::Layer::init() )
		{
			return false;
		}
		setTouchEnabled(true);
		setTouchMode(cocos2d::Touch::DispatchMode::ONE_BY_ONE);
		return true;
	}
public:
	CREATE_FUNC(FKNNTouchScence);
public:
	virtual bool onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent)
	{
		if (!utility::isAllVisible(this))
		{
			return false;
		}
		if (pTouch->getID() != 0)
		{
			return false;
		}
		return FKNNGameScence::Instance().ccTouchBegan(pTouch->getLocation());
	}
	virtual void onTouchMoved(cocos2d::Touch *pTouch, cocos2d::Event *pEvent)
	{
		if (!utility::isAllVisible(this))
		{
			return;
		}
		if (pTouch->getID() != 0)
		{
			return;
		}
		return FKNNGameScence::Instance().ccTouchMoved(pTouch->getDelta());
	}
	virtual void onTouchEnded(cocos2d::Touch *pTouch, cocos2d::Event *pEvent)
	{
		if (!utility::isAllVisible(this))
		{
			return;
		}
		if (pTouch->getID() != 0)
		{
			return;
		}
		return FKNNGameScence::Instance().ccTouchEnded(pTouch->getLocation());
	}
};

void FKNNGameScence::initTouch()
{
	FKNNTouchScence* pTouchScence = new FKNNTouchScence;
	pTouchScence->init();
	addChild(pTouchScence);
}

bool FKNNGameScence::ccTouchBegan(cocos2d::Vec2 kPos)
{
	if (!isVisible())
	{
		return false;
	}
	if (getGameState() == FKNN_STATE_PLAYING)
	{
		m_pTouchCardNode = m_pLocal->getTouchCardNode(kPos);
		if (m_pTouchCardNode)
		{
			CCardSprite* currCard = (CCardSprite*)m_pTouchCardNode;
			if (currCard->getStatus() == CCardSprite::ST_UNSELECTED && m_pLocal->getSelectedCardNum() >= 3)
			{
				return false;
			}
			currCard->selected();
			return true;
		}
	}
	return false;
}
void FKNNGameScence::ccTouchMoved(cocos2d::Vec2 kPos)
{
	//cocos2d::Vec2 kTempPos = m_pTouchCardNode->getPosition();
	//m_pTouchCardNode->setPosition(kTempPos+kPos);
}
void FKNNGameScence::ccTouchEnded(cocos2d::Vec2 kPos)
{
// 	cocos2d::Vec2 kTempPos = m_pTouchCardNode->getPosition();
// 	if (kTempPos.y != FKNNPlayer::CARD_UP_POSY || kTempPos.x !=  m_kTouchSrcPos.x)
// 	{
// 		m_pTouchCardNode->setPosition(m_kTouchSrcPos);
// 	}
// 	if (kTempPos.y > FKNNPlayer::CARD_UP_POSY)
// 	{
// 		SendOutCard(m_pTouchCardNode);
// 	}
}
/*
void FKNNGameScence::SendOutCard(cocos2d::Node* pCard)
{
	ASSERT(pCard && m_nGameState == JDNN_STATE_PLAYING);
	if (pCard == NULL || m_nGameState != JDNN_STATE_PLAYING ) return;

	CMD_C_OutCard OutCard;
	OutCard.cbCardData = m_pLocal->getTouchCardVlaue(pCard);
	SendSocketData(SUB_C_OUT_CARD,&OutCard,sizeof(OutCard));
	}*/